import ecsSystem from '../app/ecs/ecsSystem';
import jumpComponent from '../ecs-component/jumpComponent';

export default class jumpSystem extends ecsSystem {
    constructor() {
        // 此处super传入sort值, 它影响system的执行顺序, 值越小优先级越高
        super(200);
    }

    progress(ratio, min, max) {
        if (ratio <= 0) {
            ratio = 0;
        } else if (ratio >= 1) {
            ratio = 1;
        }
        return (max - min) * ratio + min;
    }

    ratio(ratio, rate = 1) {
        return Math.pow(ratio, 1 / rate);
    }

    ecsUpdate(dt: number) {
        this.ecs.getEntitys(jumpComponent).forEach((entity) => {
            const jump = entity.getComponent(jumpComponent);
            const radio = this.ratio(jump.radio, 2);

            const y = this.progress(radio, 0, jump.height);
            jump.jumpY = y;

            const scaleX = this.progress(radio, jump.downScaleX, jump.jumpScaleX);
            jump.scaleX = scaleX;

            const scaleY = this.progress(radio, jump.downScaleY, jump.jumpScaleY);
            jump.scaleY = scaleY;
        })
    }
}